﻿using System;
using System.Collections.Generic;
using NetBay.Core.BusinessEntities.Barcodes;
using NetBay.Core.Extensions;
using System.Data;
using NetBay.Core.Log;

namespace Rio.Doc.Business.Repository.Translator
{
    public class CABTranslator
    {
        #region Interface Setting

        #region Get

        /// <summary>
        /// Retrieves Id barcode.
        /// </summary>
        /// <param name="p_oIDataReader">The I data reader.</param>
        public static long GetBarCodeId(IDataReader p_oIDataReader)
        {
            //Variable initialization.
            long l_oRet = new long();

            //Log.
            NetBay.Core.Log.LogManager.Instance().LogDebug("CABTranslator.GetBarCodeId (p_oIDataReader <" + p_oIDataReader.ToString() + ">)...");

            try
            {
                l_oRet = Convert.ToInt64(p_oIDataReader["r_cab_id"].ToString());
            }
            catch (Exception ex)
            {
                //Error.
                NetBay.Core.Log.LogManager.Instance().LogError("CABTranslator.GetBarCodeId Exception [" + ex.Message + "]");
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return l_oRet;
        }

        /// <summary>
        /// Translates the barcodes.
        /// </summary>
        /// <param name="p_oIDataReader">The I data reader.</param>
        /// <returns></returns>
        public static List<Barcode> TranslateBarcodes(IDataReader p_oIDataReader)
        {
            //Variable initialization.
            var barcodes = new List<Barcode>();

            //Log.
            NetBay.Core.Log.LogManager.Instance().LogDebug("CABTranslator.TranslateBarcodes (p_oIDataReader <" + p_oIDataReader.ToString() + ">)...");

            try
            {
                using (p_oIDataReader)
                {
                    // Read all rows
                    while (p_oIDataReader.Read())
                    {
                        barcodes.Add(TranslateOneBarcode(p_oIDataReader));
                    }
                }
            }
            catch (Exception ex)
            {
                //Error.
                NetBay.Core.Log.LogManager.Instance().LogError("CABTranslator.TranslateBarcodes Exception [" + ex.Message + "]");
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return barcodes;
        }

        /// <summary>
        /// Translates the barcodes.
        /// </summary>
        /// <param name="p_oIDataReader">The I data reader.</param>
        /// <returns></returns>
        public static List<BarcodeDocumentSeparator> TranslateBarcodeDocumentSeparator(IDataReader p_oIDataReader)
        {
            //Variables initialization.
            var barcodes = new List<BarcodeDocumentSeparator>();

            //Log.
            NetBay.Core.Log.LogManager.Instance().LogDebug("CABTranslator.TranslateBarcodeDocumentSeparator (p_oIDataReader <" + p_oIDataReader.ToString() + ">)...");

            try
            {
                using (p_oIDataReader)
                {
                    // Read all rows
                    while (p_oIDataReader.Read())
                    {
                        barcodes.Add(TranslateOneBarcodeDocumentSeparator(p_oIDataReader));
                    }
                }
            }
            catch (Exception ex)
            {
                //Error.
                NetBay.Core.Log.LogManager.Instance().LogError("CABTranslator.TranslateBarcodeDocumentSeparator Exception [" + ex.Message + "]");
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return barcodes;
        }

        /// <summary>
        /// Translates the barcodes.
        /// </summary>
        /// <param name="p_oIDataReader">The I data reader.</param>
        /// <returns></returns>
        public static List<BarcodeFolderSeparator> TranslateBarcodeFolderSeparator(IDataReader p_oIDataReader)
        {
            //Variable initialization.
            var barcodes = new List<BarcodeFolderSeparator>();

            //Log.
            NetBay.Core.Log.LogManager.Instance().LogDebug("CABTranslator.TranslateBarcodeFolderSeparator (p_oIDataReader <" + p_oIDataReader.ToString() + ">)...");

            try
            {
                using (p_oIDataReader)
                {
                    // Read all rows
                    while (p_oIDataReader.Read())
                    {
                        barcodes.Add(TranslateOneBarcodeFolderSeparator(p_oIDataReader));
                    }
                }
            }
            catch (Exception ex)
            {
                //Error.
                NetBay.Core.Log.LogManager.Instance().LogError("CABTranslator.TranslateBarcodeFolderSeparator Exception [" + ex.Message + "]");
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return barcodes;
        }

        /// <summary>
        /// Translates the one.
        /// </summary>
        /// <param name="p_oIDataReader">The I data reader.</param>
        private static Barcode TranslateOneBarcode(IDataReader p_oIDataReader)
        {
            //Variables initialization.
            var barcode = new Barcode();

            //Log.
            NetBay.Core.Log.LogManager.Instance().LogDebug("CABTranslator.TranslateOneBarcode (p_oIDataReader <" + p_oIDataReader.ToString() + ">)...");

            try
            {
                barcode.Identifier = Convert.ToInt64(p_oIDataReader["r_cab_id"].ToString());
                barcode.ParamCode = p_oIDataReader["r_cab_param_code"].ToString();
                barcode.Name = p_oIDataReader["r_cab_name"].ToString();
                barcode.Description = p_oIDataReader["r_cab_description"].ToString();
                barcode.Enabled = Convert.ToBoolean(int.Parse(p_oIDataReader["r_cab_enabled"].ToString()));
                barcode.Prefix = p_oIDataReader["r_cab_prefix"].ToString();
                barcode.Type = (BarcodeType)System.Enum.Parse(typeof(BarcodeType), p_oIDataReader["r_cab_type"].ToString());
                barcode.TypeLabel = barcode.Type.szGetDescription();
                barcode.Function = (BarcodeSeparatorType)System.Enum.Parse(typeof(BarcodeSeparatorType), p_oIDataReader["r_cab_function"].ToString());
            }
            catch (Exception ex)
            {
                //Error.
                NetBay.Core.Log.LogManager.Instance().LogError("CABTranslator.TranslateOneBarcode Exception [" + ex.Message + "]");
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return barcode;
        }

        /// <summary>
        /// Translates the one.
        /// </summary>
        /// <param name="p_oIDataReader">The I data reader.</param>
        private static BarcodeDocumentSeparator TranslateOneBarcodeDocumentSeparator(IDataReader p_oIDataReader)
        {
            //Variable initialization.
            var barcode = new BarcodeDocumentSeparator();

            //Log.
            NetBay.Core.Log.LogManager.Instance().LogDebug("CABTranslator.TranslateOneBarcodeDocumentSeparator (p_oIDataReader <" + p_oIDataReader.ToString() + ">)...");

            try
            {
                barcode.Identifier = Convert.ToInt64(p_oIDataReader["r_cab_doc_id"].ToString());
                barcode.CabIdentifier = Convert.ToInt64(p_oIDataReader["r_cab_id"].ToString());
                barcode.ParamCode = p_oIDataReader["r_cab_doc_param_code"].ToString();
                barcode.OrganizationId = Convert.ToInt64(p_oIDataReader["re_id"].ToString());
                barcode.FolderTypeId = Convert.ToInt64(p_oIDataReader["rp_td_id"].ToString());
                barcode.NatureName = p_oIDataReader["r_cab_doc_nature_name"].ToString();

                if (p_oIDataReader["item_id"] != DBNull.Value)
                {
                    barcode.ItemIdentifier = Convert.ToInt64(p_oIDataReader["item_id"].ToString());
                    barcode.ItemCode = p_oIDataReader["item_code"].ToString();
                    barcode.ItemName = p_oIDataReader["item_libelle"].ToString();
                }
                else
                {
                    barcode.ItemCode = "000000000000";
                }

                barcode.PdfMerge = Convert.ToBoolean(int.Parse(p_oIDataReader["r_cab_doc_pdf_merge"].ToString()));
                barcode.DeleteBarCodePage = Convert.ToBoolean(int.Parse(p_oIDataReader["r_cab_doc_delete_cab_page"].ToString()));
                barcode.ErrorManagment = Convert.ToBoolean(int.Parse(p_oIDataReader["r_cab_doc_managment_error"].ToString()));
                barcode.DocLimit = Convert.ToBoolean(int.Parse(p_oIDataReader["r_cab_doc_doc_limit"].ToString()));
                barcode.InfLimit = Convert.ToInt32(p_oIDataReader["r_cab_doc_inferior_limit"].ToString());
                barcode.SupLimit = Convert.ToInt32(p_oIDataReader["r_cab_doc_superior_limit"].ToString());
            }
            catch (Exception ex)
            {
                //Error.
                NetBay.Core.Log.LogManager.Instance().LogError("CABTranslator.TranslateOneBarcodeDocumentSeparator Exception [" + ex.Message + "]");
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return barcode;
        }

        /// <summary>
        /// Translates the one barcode folder separator.
        /// </summary>
        /// <param name="p_oIDataReader">The I data reader.</param>
        /// <returns></returns>
        private static BarcodeFolderSeparator TranslateOneBarcodeFolderSeparator(IDataReader p_oIDataReader)
        {
            //Variable initialization.
            var barcode = new BarcodeFolderSeparator();

            //Log.
            NetBay.Core.Log.LogManager.Instance().LogDebug("CABTranslator.TranslateOneBarcodeFolderSeparator (p_oIDataReader <" + p_oIDataReader.ToString() + ">)...");

            try
            {
                barcode.Identifier = Convert.ToInt64(p_oIDataReader["r_cab_folder_id"].ToString());
                barcode.CabIdentifier = Convert.ToInt64(p_oIDataReader["r_cab_id"].ToString());
                barcode.CorrespondenceFolder = Convert.ToBoolean(int.Parse(p_oIDataReader["r_cab_folder_corresp"].ToString()));
                if (p_oIDataReader["r_cab_folder_unique_index_id"] != DBNull.Value)
                {
                    barcode.OrganizationId = Convert.ToInt64(p_oIDataReader["re_id"].ToString());
                    barcode.FolderTypeId = Convert.ToInt64(p_oIDataReader["rp_td_id"].ToString());
                    barcode.UniqueIndexIdentifier = Convert.ToInt64(p_oIDataReader["r_cab_folder_unique_index_id"].ToString());
                    barcode.UniqueIndexLabel = p_oIDataReader["unique_index_name"].ToString();
                }
                barcode.StartChar = Convert.ToInt32(p_oIDataReader["r_cab_folder_start_char"].ToString());
                barcode.EndChar = Convert.ToInt32(p_oIDataReader["r_cab_folder_end_char"].ToString());
                barcode.DeleteBarCodePage = Convert.ToBoolean(int.Parse(p_oIDataReader["r_cab_folder_delete_cab_page"].ToString()));
                barcode.ErrorManagment = Convert.ToBoolean(int.Parse(p_oIDataReader["r_cab_folder_managment_error"].ToString()));
                barcode.DocLimit = Convert.ToBoolean(int.Parse(p_oIDataReader["r_cab_folder_doc_limit"].ToString()));
                barcode.InfLimit = Convert.ToInt32(p_oIDataReader["r_cab_folder_inferior_limit"].ToString());
                barcode.SupLimit = Convert.ToInt32(p_oIDataReader["r_cab_folder_superior_limit"].ToString());
                barcode.PageLimit = Convert.ToBoolean((int.Parse(p_oIDataReader["r_cab_folder_page_limit"].ToString())));
                barcode.DocumentLimit = Convert.ToBoolean((int.Parse(p_oIDataReader["r_cab_folder_document_limit"].ToString())));
            }
            catch (Exception ex)
            {
                //Error.
                NetBay.Core.Log.LogManager.Instance().LogError("CABTranslator.TranslateOneBarcodeFolderSeparator Exception [" + ex.Message + "]");
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return barcode;
        }      

        #endregion Get
      
        #endregion Interface Setting

        #region CheckUnicityIndex

        /// <summary>Check if the index value existe</summary>
        /// <returns></returns>
        public static bool CheckUnicityIndexvalue(IDataReader p_oIDataReader)
        {
            //Variable initialization.
            Boolean l_bUniqueValue = false;

            //Log.
            NetBay.Core.Log.LogManager.Instance().LogDebug("CABTranslator.CheckUnicityIndexvalue (p_oIDataReader <" + p_oIDataReader.ToString() + ">)...");

            //Test.
            try
            {
                if (!p_oIDataReader.Read())
                {
                    l_bUniqueValue = true;
                }
            }
            catch (Exception ex)
            {
                //Error.
                NetBay.Core.Log.LogManager.Instance().LogError("CABTranslator.CheckUnicityIndexvalue Exception [" + ex.Message + "]");
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
                l_bUniqueValue = false;
            }

            return l_bUniqueValue;
        }

        #endregion CheckUnicityIndex
    }
}
